<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
             label-width="80px">
      <el-row :gutter="24">
        <el-col :span="9">
          <el-form-item label="派发类型">
            <el-select v-model="dataForm.appointType" @change="appointChange" placeholder="请选择">
              <el-option
                v-for="item in appointTypeOps"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>

        <el-col :span="9" v-if="isDirect">
          <el-form-item label="接单人" prop="taker">
            <el-select v-model="taker" filterable placeholder="请选择" @change="handleTaker" value-key="orderTakerId">
              <el-option
                v-for="item in takerList"
                :key="item.orderTakerId"
                :label="item.orderTakerDesc"
                :value="item">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>

      </el-row>

    </el-form>

    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import OperationList from './orderOperationList'

  export default {
    data() {
      return {
        visible: false,
        isDirect: false,
        dataForm: {
          id: 0,
          adminCode: '',
          customerName: '',
          customerSource: '',
          customerPhone: '',
          province: '',
          prefecture: '',
          cname: '',
          customerRequirements: '',
          appointmentTime: '',
          createTime: '',
          appointmentLocation: '',
          status: '',
          auditStatus: '',
          auditRemark: '',
          staffId: '',
          staffName: '',
          orderTakerId: '',
          orderTakerName: '',
          appointType: 'INITIATIVE',
          remark: '',
        },
        appointTypeOps: [
          {
            label: "订单直派",
            value: "APPOINT"
          },
          {
            label: "主动抢单",
            value: "INITIATIVE"
          }
        ],
        operationListVisible: true,
        taker: {
          orderTakerId: '',
          orderTakerName: ''
        },
        takerList: [{orderTakerId: '0', orderTakerName: '暂无', orderTakerDesc: '暂无'}],
        selectedOptions: [],
        adminCodeList: [],
        adminCodeDESC: {value: 'adminCode', label: 'shortName', children: 'children'},
        provinceList: [],
        prefectureList: [],
        countyList: [],
        dataRule: {
          adminCode: [
            {required: true, message: '所属辖区', trigger: 'blur'}
          ],
          customerName: [
            {required: true, message: '客户姓名不能为空', trigger: 'blur'}
          ],
          customerSource: [
            {required: true, message: '客户来源不能为空', trigger: 'blur'}
          ],
          customerPhone: [
            {required: true, message: '客户电话不能为空', trigger: 'blur'}
          ],
          customerRequirements: [
            {required: true, message: '客户需求不能为空', trigger: 'blur'}
          ],
          appointmentTime: [
            {required: true, message: '预约时间不能为空', trigger: 'blur'}
          ],
          appointmentLocation: [
            {required: true, message: '预约地址不能为空', trigger: 'blur'}
          ]
        }
      }
    },
    components: {
      OperationList
    },
    methods: {
      init(id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.getAdminCodes()
        this.taker = {}
        this.selectedOptions = []
        this.takerList = [{orderTakerId: '0', orderTakerName: '暂无', orderTakerDesc: '暂无'}]
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.operationListVisible = true
            this.$http({
              url: this.$http.adornUrl(`/quickService/order/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.adminCode = data.order.adminCode
                this.dataForm.customerName = data.order.customerName
                this.dataForm.customerSource = data.order.customerSource
                this.dataForm.customerPhone = data.order.customerPhone
                this.dataForm.customerRequirements = data.order.customerRequirements
                this.dataForm.appointmentTime = data.order.appointmentTime
                this.dataForm.appointmentLocation = data.order.appointmentLocation
                this.dataForm.remark = data.order.remark
                this.taker = {orderTakerId: data.order.orderTakerId, orderTakerName: data.order.orderTakerName}
                this.selectedOptions = data.adminCodes
                this.dataForm.appointType = data.order.appointType
                if (data.order.appointType == "APPOINT") {
                  this.isDirect = true;
                } else {
                  this.isDirect = false;
                }
              }
            });
            this.$refs.OperationList.getDataList(this.dataForm.id)
          } else {
            this.operationListVisible = false
          }
        })
      },
      appointChange() {
        if (this.dataForm.appointType == 'APPOINT') {
          this.isDirect = true;
        } else {
          this.isDirect = false;
        }
      },
      // 表单提交
      dataFormSubmit() {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/quickService/order/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'adminCode': this.dataForm.adminCode,
                'customerName': this.dataForm.customerName,
                'customerSource': this.dataForm.customerSource,
                'customerPhone': this.dataForm.customerPhone,
                'customerRequirements': this.dataForm.customerRequirements,
                'appointmentTime': this.dataForm.appointmentTime,
                'appointmentLocation': this.dataForm.appointmentLocation,
                'orderTakerId': this.taker.orderTakerId,
                'orderTakerName': this.taker.orderTakerName,
                'remark': this.dataForm.remark,
                'appointType': this.dataForm.appointType,
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      },
      getAdminCodes() {
        this.$http({
          url: this.$http.adornUrl(`/quickService/areacode/adminCodeList`),
          method: 'get',
          params: this.$http.adornParams()
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.adminCodeList = data.adminCodeList
            this.getOrderTakerList()
          }
        })
      },
      handleChange(data) {
        this.dataForm.adminCode = data[data.length - 1]
        this.taker = {}
        this.getOrderTakerList()
      },
      getOrderTakerList() {
        this.$http({
          url: this.$http.adornUrl(`/quickService/userExtra/takerList`),
          method: 'post',
          params: {
            'adminCode': this.dataForm.adminCode
          }
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.takerList = data.takerList;//this.takerList.concat(data.takerList)
          }
        })
      },
      handleTaker(data) {
        this.dataForm.orderTakerName = data.orderTakerName
      }
    }
  }
</script>
